import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CelsiusConverter implements ActionListener {
	private JFrame converterFrame;
	private JPanel converterPanel;
	private JTextField textfieldCelsius;
	private JLabel celsiusLabel, fahrenheitLabel;
	private JButton convertButton, Quit;

	public CelsiusConverter() {
		// Create and set up the window.
		converterFrame = new JFrame();
		converterFrame.setTitle("Convert Celsius to Fahrenheit");
		converterFrame.setSize(400, 600);
		converterFrame.setLocation(200, 200);
		converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and set up the panel.
		converterPanel = new JPanel(new GridLayout(1, 4));

		// Add the components.
		addComponents();

		// Set the default button.
		converterFrame.getRootPane().setDefaultButton(convertButton);
		converterFrame.getRootPane().setDefaultButton(Quit);

		// Add the panel to the window.
		converterFrame.getContentPane()
				.add(converterPanel, BorderLayout.CENTER);

		// Display the window.
		converterFrame.pack();
		converterFrame.setVisible(true);
	}

	/**
	 * Create and add the components.
	 */
	private void addComponents() {
		// Create components.
		textfieldCelsius = new JTextField(2);
		celsiusLabel = new JLabel("Celsius", SwingConstants.LEFT);
		convertButton = new JButton("Convert");
		Quit = new JButton("Quit");
		fahrenheitLabel = new JLabel("Fahrenheit          ",
				SwingConstants.LEFT);

		// Listen to events from the Convert button.
		convertButton.addActionListener(this);
		Quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				converterFrame.dispose();
			}
		});
		// Add the components to the container.
		converterPanel.add(textfieldCelsius);
		converterPanel.add(celsiusLabel);
		converterPanel.add(fahrenheitLabel);
		converterPanel.add(convertButton);
		converterPanel.add(Quit);
		

		celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
	}

	public void actionPerformed(ActionEvent event) {
		// Parse degrees Celsius as a double and convert to Fahrenheit.
				try{
					int input = Integer.parseInt(textfieldCelsius.getText());
					int tempFahr = (int) (input * 1.8 + 32);
					fahrenheitLabel.setText(tempFahr + " Fahrenheit");
				}
				catch(NumberFormatException e)
				{
					System.out.println("Invalid input");
					JOptionPane jop3 = new JOptionPane();
					jop3.showMessageDialog(null, "Error msg", "Invalid input", JOptionPane.ERROR_MESSAGE);
					converterFrame.dispose();
				}
	}

}